/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.behaviours;

import co.edu.unal.inventory.Stock;
import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import java.util.HashMap;
import javax.swing.JList;
import javax.swing.JTextArea;

/**
 *
 * @author raider
 */
public class CheckStockServer extends CyclicBehaviour {

    private HashMap catalogue;
    private Stock stock;
    private String agentType;
    private JTextArea log;
    private int price;
    JTextArea lista;

    public CheckStockServer(HashMap catalogue, Stock stock, JTextArea log, int price,String agentType, JTextArea lista) {
        this.catalogue = catalogue;
        this.stock = stock;
        this.agentType=agentType;
        this.log = log;
        this.price = price;
        this.lista = lista;
    }
    
    @Override
    public void action() {
        stock.setInventarioActual(catalogue.size());
        if (stock.getInventarioActual()<=stock.getPuntoPedido()){
            log.setText(log.getText()+"\n"+"Reload stock D: !!!!");
            block(stock.getlT()*1000);
            for (int i = 0; i < stock.getInventarioMaximo()-stock.getInventarioActual(); i++) {
                if(agentType.equals("book-manufacturer")){
                    int size = catalogue.size();
                    catalogue.put(""+i, price);
                    int count = 0;
                    while(size == catalogue.size()){
                        count++;
                        catalogue.put(""+(i+count), price);
                    }
                }else{
                    System.out.println("buscando libro "+i);
                    reloadStock(""+i, "1", price);
                }
            }
            log.setText(log.getText()+"\n"+"Stock reloaded :D ");
        }
        
            stock.setInventarioActual(catalogue.size());
            String catalogo = catalogue.toString();
            catalogo = catalogo.substring(1, catalogo.length()-1);
            catalogo = (catalogo.replace(",", "\nName")).replace("=", " price $");
            catalogo = "Name "+catalogo;
            catalogo = "Total "+stock.getInventarioActual() +"\n" +catalogo;
            lista.setText(catalogo);
        
    }
    
    public void reloadStock(final String bookRequested, final String quantity, final int price){
        AID[] sellerAgents = null;
        DFAgentDescription template = new DFAgentDescription();
        ServiceDescription sd = new ServiceDescription();
        sd.setType(agentType.equals("book-seller") ? "book-distributor" : "book-manufacturer");
        template.addServices(sd);
        try {
            DFAgentDescription[] result = DFService.search(myAgent, template);
            sellerAgents = new AID[result.length];
            for (int i = 0; i < result.length; ++i) {
                sellerAgents[i] = result[i].getName();
            }
        } catch (FIPAException fe) {
            log.setText(log.getText()+ "\n" +"fe.getMessage() = " + fe.getMessage());
            System.out.println("fe.getMessage() = " + fe.getMessage());
        }
        System.out.println("    seller "+sellerAgents);
        myAgent.addBehaviour(new RequestPerformer(sellerAgents, bookRequested, quantity, log));
    }
}
